import numpy as np
import pandas as pd
import webbrowser

'''
ReIndex
'''


def main():
    df1 = pd.Series([1, 2, 3, 4], index=['A', 'B', 'C', 'D'])
    '''
    A    1
    B    2
    C    3
    D    4
    dtype: int64
    '''
    print(df1)
    '''
    B    2
    C    3
    D    4
    dtype: int64
    '''
    print(df1.drop('A'))
    '''
    A    1.0
    B    2.0
    C    3.0
    D    4.0
    E    NaN
    dtype: float64
    '''
    print(df1.reindex(index=['A', 'B', 'C', 'D', 'E']))
    '''
    填充缺失元素
    A     1
    B     2
    C     3
    D     4
    E    10
    dtype: int64
    '''
    print(df1.reindex(index=['A', 'B', 'C', 'D', 'E'], fill_value=10))

    df2 = pd.Series(['sss', 'ccc', 'ttt'], index=[1, 5, 10])
    '''
    1     sss
    5     ccc
    10    ttt
    dtype: object
    '''
    print(df2)

    '''
    0     NaN
    1     sss
    2     NaN
    3     NaN
    4     NaN
    5     ccc
    6     NaN
    7     NaN
    8     NaN
    9     NaN
    10    ttt
    11    NaN
    12    NaN
    13    NaN
    14    NaN
    dtype: object
    '''
    print(df2.reindex(index=range(15)))
    '''
    区间进行填充
    0     NaN
    1     sss
    2     sss
    3     sss
    4     sss
    5     ccc
    6     ccc
    7     ccc
    8     ccc
    9     ccc
    10    ttt
    11    ttt
    12    ttt
    13    ttt
    14    ttt
    dtype: object
    '''
    print(df2.reindex(index=range(15), method='ffill'))

    '''
    DataFrame
    '''
    df3 = pd.DataFrame(np.random.rand(25).reshape([5, 5]), index=['A', 'B', 'D', 'E', 'F'],
                       columns=['c1', 'c2', 'c3', 'c4', 'c5'])
    '''
             c1        c2        c3        c4        c5
    A  0.306784  0.948941  0.910302  0.815610  0.327317
    B  0.077113  0.546699  0.481182  0.652154  0.885725
    D  0.311500  0.229284  0.659335  0.790087  0.776873
    E  0.741663  0.713935  0.860205  0.065059  0.884537
    F  0.644633  0.781227  0.110227  0.355920  0.950699
    '''
    print(df3)
    '''
             c1        c2        c3        c4        c5  c6
    A  0.305622  0.732668  0.417368  0.775437  0.535495 NaN
    B  0.138679  0.003514  0.957646  0.140596  0.881191 NaN
    C       NaN       NaN       NaN       NaN       NaN NaN
    D  0.218124  0.300770  0.091255  0.618472  0.645816 NaN
    E  0.263873  0.603380  0.464532  0.517084  0.894111 NaN
    F  0.961358  0.375120  0.061049  0.191526  0.139341 NaN
    '''
    print(df3.reindex(index=['A', 'B', 'C', 'D', 'E', 'F'], columns=['c1', 'c2', 'c3', 'c4', 'c5', 'c6']))



    # 删除行
    '''
             c1        c2        c3        c4        c5
    B  0.901199  0.512952  0.423225  0.397357  0.619539
    D  0.244162  0.567737  0.484609  0.865524  0.652205
    E  0.407926  0.249630  0.754968  0.297765  0.385942
    F  0.150012  0.899082  0.031078  0.972249  0.955528
    '''
    print(df3.drop('A', axis=0))
    # 删除列
    '''
             c2        c3        c4        c5
    A  0.823581  0.729011  0.220025  0.832203
    B  0.512952  0.423225  0.397357  0.619539
    D  0.567737  0.484609  0.865524  0.652205
    E  0.249630  0.754968  0.297765  0.385942
    F  0.899082  0.031078  0.972249  0.955528
    '''
    print(df3.drop('c1', axis=1))

if __name__ == '__main__':
    main()
